home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch7 / Enlarge.frm < prev    next >
Text File  |  1999-05-04  |  13KB  |  409 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmEnlarge 
  4.    Caption         =   "Enlarge []"
  5.    ClientHeight    =   2895
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   3120
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   2895
  11.    ScaleWidth      =   3120
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.PictureBox picResult 
  14.       Height          =   2295
  15.       Left            =   840
  16.       ScaleHeight     =   149
  17.       ScaleMode       =   3  'Pixel
  18.       ScaleWidth      =   157
  19.       TabIndex        =   4
  20.       Top             =   1440
  21.       Visible         =   0   'False
  22.       Width           =   2415
  23.    End
  24.    Begin VB.CommandButton cmdEnlarge 
  25.       Caption         =   "Enlarge"
  26.       Default         =   -1  'True
  27.       Height          =   375
  28.       Left            =   1200
  29.       TabIndex        =   3
  30.       Top             =   0
  31.       Width           =   855
  32.    End
  33.    Begin VB.TextBox txtScale 
  34.       Height          =   285
  35.       Left            =   600
  36.       TabIndex        =   2
  37.       Text            =   "1.0"
  38.       Top             =   60
  39.       Width           =   495
  40.    End
  41.    Begin MSComDlg.CommonDialog dlgOpenFile 
  42.       Left            =   0
  43.       Top             =   360
  44.       _ExtentX        =   847
  45.       _ExtentY        =   847
  46.       _Version        =   393216
  47.    End
  48.    Begin VB.PictureBox picOriginal 
  49.       AutoSize        =   -1  'True
  50.       Height          =   2295
  51.       Left            =   120
  52.       ScaleHeight     =   149
  53.       ScaleMode       =   3  'Pixel
  54.       ScaleWidth      =   157
  55.       TabIndex        =   0
  56.       Top             =   480
  57.       Width           =   2415
  58.    End
  59.    Begin VB.Label Label1 
  60.       Caption         =   "Scale"
  61.       Height          =   255
  62.       Left            =   120
  63.       TabIndex        =   1
  64.       Top             =   60
  65.       Width           =   495
  66.    End
  67.    Begin VB.Menu mnuFile 
  68.       Caption         =   "&File"
  69.       Begin VB.Menu mnuFileOpen 
  70.          Caption         =   "&Open..."
  71.          Shortcut        =   ^O
  72.       End
  73.       Begin VB.Menu mnuFileSaveAs 
  74.          Caption         =   "Save &As..."
  75.          Shortcut        =   ^A
  76.       End
  77.    End
  78. End
  79. Attribute VB_Name = "frmEnlarge"
  80. Attribute VB_GlobalNameSpace = False
  81. Attribute VB_Creatable = False
  82. Attribute VB_PredeclaredId = True
  83. Attribute VB_Exposed = False
  84. Option Explicit
  85.  
  86. Private FromXmin As Single
  87. Private FromYmin As Single
  88. Private ToXmin As Single
  89. Private ToYmin As Single
  90. Private XScale As Single
  91. Private YScale As Single
  92.  
  93. ' Copy the picture.
  94. Private Sub EnlargePicture(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox, ByVal from_xmin As Single, ByVal from_ymin As Single, ByVal from_wid As Single, ByVal from_hgt As Single, ByVal to_xmin As Single, ByVal to_ymin As Single, ByVal to_wid As Single, ByVal to_hgt As Single)
  95.     ' Save mapping values.
  96.     FromXmin = from_xmin
  97.     FromYmin = from_ymin
  98.     ToXmin = to_xmin
  99.     ToYmin = to_ymin
  100.     XScale = to_wid / (from_wid - 1)
  101.     YScale = to_hgt / (from_hgt - 1)
  102.  
  103.     ' Transform the image.
  104.     TransformImage pic_from, pic_to
  105. End Sub
  106.  
  107. ' Map the output pixel (ix_out, iy_out) to the input
  108. ' pixel (x_in, y_in).
  109. Private Sub MapPixel(ByVal ix_out As Single, ByVal iy_out As Single, ByRef x_in As Single, ByRef y_in As Single)
  110.     x_in = FromXmin + (ix_out - ToXmin) / XScale
  111.     y_in = FromYmin + (iy_out - ToYmin) / YScale
  112. End Sub
  113.  
  114. ' Transform the image.
  115. Private Sub TransformImage(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox)
  116. Dim white_pixel As RGBTriplet
  117. Dim input_pixels() As RGBTriplet
  118. Dim result_pixels() As RGBTriplet
  119. Dim bits_per_pixel As Integer
  120. Dim ix_max As Single
  121. Dim iy_max As Single
  122. Dim x_in As Single
  123. Dim y_in As Single
  124. Dim ix_out As Integer
  125. Dim iy_out As Integer
  126. Dim ix_in As Integer
  127. Dim iy_in As Integer
  128. Dim dx As Single
  129. Dim dy As Single
  130. Dim dx1 As Single
  131. Dim dx2 As Single
  132. Dim dy1 As Single
  133. Dim dy2 As Single
  134. Dim v11 As Integer
  135. Dim v12 As Integer
  136. Dim v21 As Integer
  137. Dim v22 As Integer
  138.  
  139.     ' Set the white pixel's value.
  140.     With white_pixel
  141.         .rgbRed = 255
  142.         .rgbGreen = 255
  143.         .rgbBlue = 255
  144.     End With
  145.  
  146.     ' Get the pixels from pic_from.
  147.     GetBitmapPixels pic_from, input_pixels, bits_per_pixel
  148.  
  149.     ' Get the pixels from pic_to.
  150.     GetBitmapPixels pic_to, result_pixels, bits_per_pixel
  151.  
  152.     ' Get the original image's bounds.
  153.     ix_max = pic_from.ScaleWidth - 2
  154.     iy_max = pic_from.ScaleHeight - 2
  155.  
  156.     ' Calculate the output pixel values.
  157.     For iy_out = 0 To pic_to.ScaleHeight - 1
  158.         For ix_out = 0 To pic_to.ScaleWidth - 1
  159.             ' Map the pixel value from
  160.             ' (ix_out, iy_out) to (x_in, y_in).
  161.             MapPixel ix_out, iy_out, x_in, y_in
  162.  
  163.             ' Interpolate to find the pixel's value.
  164.             ' Find the nearest integral position.
  165.             ix_in = Int(x_in)
  166.             iy_in = Int(y_in)
  167.  
  168.             ' See if this is out of bounds.
  169.             If (ix_in < 0) Or (ix_in > ix_max) Or _
  170.                (iy_in < 0) Or (iy_in > iy_max) _
  171.             Then
  172.                 ' The point is outside the image.
  173.                 ' Use white.
  174.                 result_pixels(ix_out, iy_out) = white_pixel
  175.             Else
  176.                 ' The point lies within the image.
  177.                 ' Calculate its value.
  178.                 dx1 = x_in - ix_in
  179.                 dy1 = y_in - iy_in
  180.                 dx2 = 1# - dx1
  181.                 dy2 = 1# - dy1
  182.  
  183.                 With result_pixels(ix_out, iy_out)
  184.                     ' Calculate the red value.
  185.                     v11 = input_pixels(ix_in, iy_in).rgbRed
  186.                     v12 = input_pixels(ix_in, iy_in + 1).rgbRed
  187.                     v21 = input_pixels(ix_in + 1, iy_in).rgbRed
  188.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbRed
  189.                     .rgbRed = _
  190.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  191.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  192.         
  193.                     ' Calculate the green value.
  194.                     v11 = input_pixels(ix_in, iy_in).rgbGreen
  195.                     v12 = input_pixels(ix_in, iy_in + 1).rgbGreen
  196.                     v21 = input_pixels(ix_in + 1, iy_in).rgbGreen
  197.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbGreen
  198.                     .rgbGreen = _
  199.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  200.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  201.  
  202.                     ' Calculate the blue value.
  203.                     v11 = input_pixels(ix_in, iy_in).rgbBlue
  204.                     v12 = input_pixels(ix_in, iy_in + 1).rgbBlue
  205.                     v21 = input_pixels(ix_in + 1, iy_in).rgbBlue
  206.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbBlue
  207.                     .rgbBlue = _
  208.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  209.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  210.                 End With
  211.             End If
  212.         Next ix_out
  213.     Next iy_out
  214.  
  215.     ' Set pic_to's pixels.
  216.     SetBitmapPixels pic_to, bits_per_pixel, result_pixels
  217.     pic_to.Picture = pic_to.Image
  218. End Sub
  219. ' Arrange the controls.
  220. Private Sub ArrangeControls(ByVal scale_factor As Single)
  221. Dim new_wid As Single
  222. Dim new_hgt As Single
  223.  
  224.     ' Calculate the result's size.
  225.     new_wid = picOriginal.ScaleWidth * scale_factor
  226.     new_hgt = picOriginal.ScaleHeight * scale_factor
  227.     new_wid = ScaleX(new_wid, vbPixels, ScaleMode) + picOriginal.Width - ScaleX(picOriginal.ScaleWidth, vbPixels, ScaleMode)
  228.     new_hgt = ScaleY(new_hgt, vbPixels, ScaleMode) + picOriginal.Height - ScaleY(picOriginal.ScaleHeight, vbPixels, ScaleMode)
  229.  
  230.     ' Position the result PictureBox.
  231.     picResult.Move _
  232.         picOriginal.Left + picOriginal.Width + 120, _
  233.         picOriginal.Top, new_wid, new_hgt
  234.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  235.         picResult.BackColor, BF
  236.     picResult.Picture = picResult.Image
  237.     picResult.Visible = True
  238.  
  239.     ' This makes the image resize itself to
  240.     ' fit the picture.
  241.     picResult.Picture = picResult.Image
  242.  
  243.     ' Make the form big enough.
  244.     new_wid = picResult.Left + picResult.Width
  245.     If new_wid < cmdEnlarge.Left + cmdEnlarge.Width _
  246.         Then new_wid = cmdEnlarge.Left + cmdEnlarge.Width
  247.     new_hgt = picResult.Top + picResult.Height
  248.     Move Left, Top, new_wid + 237, new_hgt + 816
  249.  
  250.     DoEvents
  251. End Sub
  252.  
  253. ' Transform the picture.
  254. Private Sub cmdEnlarge_Click()
  255. Dim scale_factor As Single
  256.  
  257.     ' Do nothing if no picture is loaded.
  258.     If picOriginal.Picture = 0 Then Exit Sub
  259.  
  260.     ' Get the scale.
  261.     On Error GoTo ScaleError
  262.     scale_factor = CSng(txtScale.Text)
  263.     On Error GoTo 0
  264.  
  265.     ' Make sure the scale is at least 1.
  266.     If scale_factor < 1# Then
  267.         MsgBox "Scale must be at least 1.0"
  268.         txtScale.SetFocus
  269.         Exit Sub
  270.     End If
  271.  
  272.     Screen.MousePointer = vbHourglass
  273.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  274.         picResult.BackColor, BF
  275.     DoEvents
  276.  
  277.     ' Arrange picResult.
  278.     ArrangeControls scale_factor
  279.  
  280.     ' Transform the image.
  281.     EnlargePicture picOriginal, picResult, _
  282.         0, 0, _
  283.         picOriginal.ScaleWidth, picOriginal.ScaleHeight, _
  284.         0, 0, _
  285.         picResult.ScaleWidth, picResult.ScaleHeight
  286.  
  287.     Screen.MousePointer = vbDefault
  288.     Exit Sub
  289.  
  290. ScaleError:
  291.     MsgBox "Invalid scale"
  292.     txtScale.SetFocus
  293. End Sub
  294.  
  295. ' Start in the current directory.
  296. Private Sub Form_Load()
  297.     picOriginal.AutoSize = True
  298.     picOriginal.ScaleMode = vbPixels
  299.     picOriginal.AutoRedraw = True
  300.     picResult.ScaleMode = vbPixels
  301.     picResult.AutoRedraw = True
  302.  
  303.     dlgOpenFile.CancelError = True
  304.     dlgOpenFile.InitDir = App.Path
  305.     dlgOpenFile.Filter = _
  306.         "Bitmaps (*.bmp)|*.bmp|" & _
  307.         "GIFs (*.gif)|*.gif|" & _
  308.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  309.         "Icons (*.ico)|*.ico|" & _
  310.         "Cursors (*.cur)|*.cur|" & _
  311.         "Run-Length Encoded (*.rle)|*.rle|" & _
  312.         "Metafiles (*.wmf)|*.wmf|" & _
  313.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  314.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  315.         "All Files (*.*)|*.*"
  316.  
  317.     Width = picResult.Left + picResult.Width + 120 + Width - ScaleWidth
  318.     Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
  319. End Sub
  320. ' Load the indicated file.
  321. Private Sub mnuFileOpen_Click()
  322. Dim file_name As String
  323.  
  324.     ' Let the user select a file.
  325.     On Error Resume Next
  326.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  327.     dlgOpenFile.ShowOpen
  328.     If Err.Number = cdlCancel Then
  329.         Exit Sub
  330.     ElseIf Err.Number <> 0 Then
  331.         Beep
  332.         MsgBox "Error selecting file.", , vbExclamation
  333.         Exit Sub
  334.     End If
  335.     On Error GoTo 0
  336.  
  337.     Screen.MousePointer = vbHourglass
  338.     DoEvents
  339.  
  340.     file_name = Trim$(dlgOpenFile.FileName)
  341.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  342.         - Len(dlgOpenFile.FileTitle) - 1)
  343.     Caption = "Enlarge [" & dlgOpenFile.FileTitle & "]"
  344.  
  345.     ' Open the original file.
  346.     On Error GoTo LoadError
  347.     picOriginal.Picture = LoadPicture(file_name)
  348.     On Error GoTo 0
  349.  
  350.     ' Hide picResult.
  351.     picResult.Visible = False
  352.     If cmdEnlarge.Left + cmdEnlarge.Width > picOriginal.Left + picOriginal.Width Then
  353.         Width = cmdEnlarge.Left + cmdEnlarge.Width + 120 + Width - ScaleWidth
  354.     Else
  355.         Width = picOriginal.Left + picOriginal.Width + 120 + Width - ScaleWidth
  356.     End If
  357.     Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
  358.  
  359.     Screen.MousePointer = vbDefault
  360.     Exit Sub
  361.  
  362. LoadError:
  363.     Screen.MousePointer = vbDefault
  364.     MsgBox "Error " & Format$(Err.Number) & _
  365.         " opening file '" & file_name & "'" & vbCrLf & _
  366.         Err.Description
  367. End Sub
  368.  
  369. ' Save the transformed image.
  370. Private Sub mnuFileSaveAs_Click()
  371. Dim file_name As String
  372.  
  373.     ' Let the user select a file.
  374.     On Error Resume Next
  375.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  376.     dlgOpenFile.ShowSave
  377.     If Err.Number = cdlCancel Then
  378.         Exit Sub
  379.     ElseIf Err.Number <> 0 Then
  380.         Beep
  381.         MsgBox "Error selecting file.", , vbExclamation
  382.         Exit Sub
  383.     End If
  384.     On Error GoTo 0
  385.  
  386.     Screen.MousePointer = vbHourglass
  387.     DoEvents
  388.  
  389.     file_name = Trim$(dlgOpenFile.FileName)
  390.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  391.         - Len(dlgOpenFile.FileTitle) - 1)
  392.     Caption = "Enlarge [" & dlgOpenFile.FileTitle & "]"
  393.  
  394.     ' Save the transformed image into the file.
  395.     On Error GoTo SaveError
  396.     SavePicture picResult.Picture, file_name
  397.     On Error GoTo 0
  398.  
  399.     Screen.MousePointer = vbDefault
  400.     Exit Sub
  401.  
  402. SaveError:
  403.     Screen.MousePointer = vbDefault
  404.     MsgBox "Error " & Format$(Err.Number) & _
  405.         " saving file '" & file_name & "'" & vbCrLf & _
  406.         Err.Description
  407. End Sub
  408.  
  409.